package com.project.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dtyunxi.rest.RestCode;
import com.dtyunxi.rest.RestResponse;
import com.project.common.Constants;
import com.project.entity.user.User;
import com.project.service.IAreaService;
import com.project.service.IUserService;
import com.project.util.EhcacheUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("api/user")
@Api(value = "api/user", description = "用户管理")
public class UserController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private IUserService userService;
	@Autowired
	private IAreaService areaService;
	
	@ApiOperation(value = "修改用户信息", notes = "修改用户信息(id必填)",response = Long.class)
    @RequestMapping(value = "update", method = RequestMethod.POST)
	public RestResponse updateUser(HttpServletRequest request,
			@ApiParam(required = true, name = "user", value = "用户信息")@ModelAttribute User user){
		if(user.getId()==null)
			return new RestResponse(RestCode.RESULT_SYSTEM_ERROR,"id不能为空");
		if(StringUtils.isNotBlank(user.getMobile())){
			boolean isExist = userService.verifyMobileExists(user.getId(), user.getMobile());
			if(isExist)
				return new RestResponse(RestCode.RESULT_SYSTEM_ERROR,"手机号已存在");
			user.setUserCode(user.getMobile());
		}else{
			return new RestResponse(RestCode.RESULT_SYSTEM_ERROR,"手机号不能为空");
		}
		userService.update(user);
		String auth = request.getHeader("auth");
		logger.info("------------------updateUser修改用户信息.更新缓存auth:{}", auth);
		EhcacheUtil.put(Constants.LOGIN_USER_CACHE, auth, user);
		request.getSession().setAttribute(Constants.CURRENT_LOGIN_USER, user);
		return new RestResponse(user.getId());
	}
	
	@ApiOperation(value = "获取用户信息", notes = "获取用户信息",response = User.class)
    @RequestMapping(value = "ops", method = RequestMethod.GET)
	public RestResponse getUser(HttpServletRequest request){
		User user = (User)request.getSession().getAttribute(Constants.CURRENT_LOGIN_USER);
		if(user!=null){
			if (StringUtils.isNotEmpty(user.getProvince()))
				user.setProvinceName(areaService.getAreaNameByCode(user.getProvince()));
			if (StringUtils.isNotEmpty(user.getCity()))
				user.setCityName(areaService.getAreaNameByCode(user.getCity()));
            if (StringUtils.isNotEmpty(user.getDistrict()))
            	user.setDistrictName(areaService.getAreaNameByCode(user.getDistrict()));
		}
		return new RestResponse(user);
	}
	
	
}
